package com.woniuxy.mesboot.service.impl;

import cn.hutool.core.date.DateTime;
import com.woniuxy.mesboot.dto.WorkTicketsDTO;
import com.woniuxy.mesboot.entity.*;
import com.woniuxy.mesboot.mapper.*;
import com.woniuxy.mesboot.service.MatInProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MatInProductionServiceImpl implements MatInProductionService {
    @Autowired
    private MesMatInProductionMapper inProductionMapper;
    @Autowired
    private MesTicketdetailMapper ticketdetail;
    @Autowired
    private MesProcessDetailsMapper processDetailsMapper;
    @Autowired
    private MatStorageMapper matStorageMapper;
    @Autowired
    private MatOutMapper matOutMapper;


    @Transactional
    @Override
    public int product(WorkTicketsDTO ticketsDTO) {
//获取需求产品数，产品id
        Long ticketId = ticketsDTO.getTicketId();
        MesTicketdetail ticketdetail = this.ticketdetail.selectTicketId(ticketId);
        Integer count = ticketdetail.getCount();
        String productId = ticketdetail.getProductId();
        int proId = Integer.parseInt(productId);
//计算需要的物料数量
        MesProcessDetails processDetails = processDetailsMapper.selectMatNumByOperationProcedureId(proId);
        Integer matNum = processDetails.getMatNum();
        String workNo = processDetails.getWorkNo();
        int total = matNum * count;
//判断库存是否满足需求
        MatStorage matStorage = matStorageMapper.selectByPrimaryKey((long) proId);
        Integer matCount = matStorage.getMatNum();
        if (matCount < total) {
            return 0;
        } else {
            // 物料出库,生产物料出库表
            MatOut matOut = new MatOut();
            matOut.setMatOutNum(total);
            DateTime dateTime = new DateTime();
            matOut.setOutDate(dateTime);
            matOut.setMatId(proId);
            matOut.setWarehouseId(matStorage.getWarehouseId());
            matOut.setWorkNo(ticketsDTO.getWorkNo());
            matOutMapper.insertSelective(matOut);
            Integer matOutId = matOut.getMatOutId();
            // 库存减少
            matStorage.setMatNum(matCount - total);
            matStorageMapper.updateByPrimaryKeySelective(matStorage);

            // 生产领料
            MesMatInProduction matInProduction = new MesMatInProduction();
            matInProduction.setMatId(proId);
            matInProduction.setMatInCount(total);
            matInProduction.setMatOutId(matOutId);
            matInProduction.setWorkNo(workNo);
            inProductionMapper.insertSelective(matInProduction);
            return 1;
        }
    }

    @Override
    public List<MesMatInProduction> selectAll() {
        return inProductionMapper.selectAll();
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return inProductionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(MesMatInProduction record) {
        return inProductionMapper.insert(record);
    }

    @Override
    public int insertSelective(MesMatInProduction record) {
        return inProductionMapper.insertSelective(record);
    }

    @Override
    public MesMatInProduction selectByPrimaryKey(Long id) {
        return inProductionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(MesMatInProduction record) {
        return inProductionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MesMatInProduction record) {
        return inProductionMapper.updateByPrimaryKey(record);
    }
}
